package vcp.views;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;

import vcp.common.AbstractView;
import vcp.common.IView;
import vcp.gui.components.VCPMenu;
import vcp.gui.components.VCPPanel;
import vcp.gui.forms.CancelOrderForm;
import vcp.gui.forms.LoginForm;
import vcp.gui.panels.TopMainScreenView;
import vcp.gui.utils.MenuBuilder;



public class MainView extends AbstractView implements Observer,IView {	
	HashMap<String, Object> hm = new HashMap<String, Object>();
	
	private int _projectWidth = Integer.parseInt((getSettings().getProperty("ProjectWidth")));
	private int _projectHeight = Integer.parseInt((getSettings().getProperty("ProjectHeight")));
	private int _menuSidebarWidth = Integer.parseInt((getSettings().getProperty("MenuSidebarWidth")));
	private int _menuSidebarHeight = Integer.parseInt((getSettings().getProperty("MenuSidebarHeight")));
	
	
	private TopMainScreenView topScreen;
	private VCPPanel pnlLeft;
	private VCPPanel pnlRight;
	private VCPPanel pnlSwitch;
	
	private JButton MenuItmNewOrder;
	private JButton MenuItmCanellOrder;
	private JButton MenuItmComplaint;
	private JButton MenuItmTrackOrder;
	private JButton MenuItmSaveParking;
	
	
	//private ScreenOrders screenOrders;

	LoginView loginView;
	
	
	
	public MainView()
	{	
		initView();	
	}	
	
	@Override
	public void update(Observable args, Object obj) {
		String s=obj.toString();
		setRightPanelView(s);
	}
	
	/* reference controller */
	public void addController(Object obj) {
		MenuItmNewOrder.addActionListener((ActionListener)obj);
		MenuItmNewOrder.setActionCommand("MenuItmNewOrder");
		MenuItmCanellOrder.addActionListener((ActionListener)obj);
		MenuItmCanellOrder.setActionCommand("MenuItmCanellOrder");
		MenuItmComplaint.addActionListener((ActionListener)obj);
		MenuItmComplaint.setActionCommand("MenuItmComplaint");
		MenuItmTrackOrder.addActionListener((ActionListener)obj);
		MenuItmTrackOrder.setActionCommand("MenuItmTrackOrder");
		MenuItmSaveParking.addActionListener((ActionListener)obj);
		MenuItmSaveParking.setActionCommand("MenuItmSaveParking");
	}
	
	private void initView(){		
		getMainFrame().setPreferredSize(
				new Dimension(_projectWidth,_projectHeight)
		);
        getMainFrame().addWindowListener(new CloseListener());		
        getMainPanel().setLayout(new BorderLayout());
        
        topScreen = new TopMainScreenView();
        getMainPanel().add(topScreen, BorderLayout.PAGE_START);
        
        pnlLeft = new VCPPanel();        
        pnlRight = new VCPPanel();
        pnlSwitch = new VCPPanel();
        
        pnlSwitch.add(((LoginView)getView("LoginView")).getLoginForm());
        
        pnlLeft.setPreferredSize(new Dimension(_menuSidebarWidth, _menuSidebarHeight));
        pnlLeft.setBackground(Color.gray);
        pnlLeft.add(getMenu());
        
        pnlRight.setBackground(Color.white);
        pnlRight.setLayout(new BorderLayout());
        pnlRight.add(pnlSwitch,BorderLayout.CENTER);
        
        getMainPanel().add(pnlLeft, BorderLayout.LINE_START);
        getMainPanel().add(pnlRight, BorderLayout.CENTER);
        
		getMainFrame().setContentPane(getMainPanel()); 
		getMainFrame().pack();
		getMainFrame().setVisible(true);    
	
	}
	
	private class CloseListener extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			e.getWindow().setVisible(false);
			System.exit(0);
		} 
	}
	
	public void setRightPanelView(String name){		
        pnlSwitch.removeAll();
		if(name.equals("MenuItmNewOrder"))
		      pnlSwitch.add(((OrderGuestView)getView("OrdersGuestView")).getWidgetPanel());
		else if(name.equals("MenuItmCanellOrder"))
			 pnlSwitch.add(((OrderGuestView)getView("OrdersGuestView")).getCancelOrderViewForm()); 
		else if(name.equals("MenuItmTrackOrder"))
			 pnlSwitch.add(((OrderGuestView)getView("OrdersGuestView")).getOrderTrackViewForm()); 
		
		getMainFrame().pack();
	}
	
	
	private VCPMenu getMenu() {
		MenuBuilder builder = new MenuBuilder(_menuSidebarWidth,_menuSidebarHeight);
		builder.addItem(getItmNewOrder("New Order"));
		builder.addItem(getMenuItmCanellOrder("Cancell Order"));
		builder.addItem(getMenuItmTrackOrder("Track Order"));
		builder.addItem(getMenuItmSaveParking("Save Parking"));		
		builder.addItem(getMenuItmComplaint("Complaints"));				
		return (VCPMenu) builder.getMenu();
	}
	
	
/* main menu components */	
	private JButton getItmNewOrder(String text) {
		if (MenuItmNewOrder == null) {
			MenuItmNewOrder = new JButton(text);
			
		}
		return MenuItmNewOrder;
	}
	
	private JButton getMenuItmCanellOrder(String text) {
		if (MenuItmCanellOrder == null) {
			MenuItmCanellOrder = new JButton(text);
			
		}
		return MenuItmCanellOrder;
	}
	
	private JButton getMenuItmComplaint(String text) {
		if (MenuItmComplaint == null) {
			MenuItmComplaint = new JButton(text);
		
		}
		return MenuItmComplaint;
	}
	
	private JButton getMenuItmTrackOrder(String text) {
		if (MenuItmTrackOrder == null) {
			MenuItmTrackOrder = new JButton(text);
			
		}
		return MenuItmTrackOrder;
	}
	
	private JButton getMenuItmSaveParking(String text) {
		if (MenuItmSaveParking == null) {
			MenuItmSaveParking = new JButton(text);			
		}
		return MenuItmSaveParking;
	}

	public void registerView(String name, Object view) {
		hm.put(name, view);		
	}

	public Object getView(String name) {
		return hm.get(name);	
	}
	
	
	
	
}
